﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RuoVea.ExUtil.ExString;

/// <summary>
/// 描 述：字符串工具方法
/// </summary>
public sealed partial class Str
{
    #region Empty(空字符串)

    /// <summary>
    /// 空字符串
    /// </summary>
    public static string Empty
    {
        get { return string.Empty; }
    }

    #endregion

    #region PinYin(获取汉字的拼音简码)
    /// <summary>
    /// 获取汉字的拼音简码，即首字母缩写,范例：中国,返回zg
    /// </summary>
    /// <param name="chineseText">汉字文本,范例： 中国</param>
    /// <returns>拼音简码</returns>
    public static string PinYin(string chineseText)
    {
        if (string.IsNullOrWhiteSpace(chineseText))
            return string.Empty;
        var result = new StringBuilder();
        foreach (char text in chineseText)
            result.AppendFormat("{0}", ResolvePinYin(text));
        return result.ToString().ToLower();
    }

    /// <summary>
    /// 解析单个汉字的拼音简码
    /// </summary>
    /// <param name="text">单个汉字</param>
    /// <returns>拼音简码</returns>
    private static string ResolvePinYin(char text)
    {
        byte[] charBytes = Encoding.Default.GetBytes(text.ToString());
        if (charBytes[0] <= 127)
            return text.ToString();
        var unicode = (ushort)(charBytes[0] * 256 + charBytes[1]);
        string pinYin = ResolvePinYinByCode(unicode);
        if (!string.IsNullOrWhiteSpace(pinYin))
            return pinYin;
        return ResolvePinYinByFile(text.ToString());
    }

    /// <summary>
    /// 使用字符编码方式获取拼音简码
    /// </summary>
    /// <param name="unicode">unicode</param>
    /// <returns>拼音简码</returns>
    private static string ResolvePinYinByCode(ushort unicode)
    {
        if (unicode >= '\uB0A1' && unicode <= '\uB0C4')
            return "A";
        if (unicode >= '\uB0C5' && unicode <= '\uB2C0' && unicode != 45464)
            return "B";
        if (unicode >= '\uB2C1' && unicode <= '\uB4ED')
            return "C";
        if (unicode >= '\uB4EE' && unicode <= '\uB6E9')
            return "D";
        if (unicode >= '\uB6EA' && unicode <= '\uB7A1')
            return "E";
        if (unicode >= '\uB7A2' && unicode <= '\uB8C0')
            return "F";
        if (unicode >= '\uB8C1' && unicode <= '\uB9FD')
            return "G";
        if (unicode >= '\uB9FE' && unicode <= '\uBBF6')
            return "H";
        if (unicode >= '\uBBF7' && unicode <= '\uBFA5')
            return "J";
        if (unicode >= '\uBFA6' && unicode <= '\uC0AB')
            return "K";
        if (unicode >= '\uC0AC' && unicode <= '\uC2E7')
            return "L";
        if (unicode >= '\uC2E8' && unicode <= '\uC4C2')
            return "M";
        if (unicode >= '\uC4C3' && unicode <= '\uC5B5')
            return "N";
        if (unicode >= '\uC5B6' && unicode <= '\uC5BD')
            return "O";
        if (unicode >= '\uC5BE' && unicode <= '\uC6D9')
            return "P";
        if (unicode >= '\uC6DA' && unicode <= '\uC8BA')
            return "Q";
        if (unicode >= '\uC8BB' && unicode <= '\uC8F5')
            return "R";
        if (unicode >= '\uC8F6' && unicode <= '\uCBF9')
            return "S";
        if (unicode >= '\uCBFA' && unicode <= '\uCDD9')
            return "T";
        if (unicode >= '\uCDDA' && unicode <= '\uCEF3')
            return "W";
        if (unicode >= '\uCEF4' && unicode <= '\uD188')
            return "X";
        if (unicode >= '\uD1B9' && unicode <= '\uD4D0')
            return "Y";
        if (unicode >= '\uD4D1' && unicode <= '\uD7F9')
            return "Z";
        return string.Empty;
    }

    /// <summary>
    /// 从拼音简码文件获取
    /// </summary>
    /// <param name="text">单个汉字</param>
    /// <returns>string</returns>
    public static string ResolvePinYinByFile(string text)
    {
        int index = Const.ChinesePinYin.IndexOf(text, StringComparison.Ordinal);
        if (index < 0)
            return string.Empty;
        return Const.ChinesePinYin.Substring(index + 1, 1);
    }

    #endregion

    #region Unique(获取全局唯一值)

    /// <summary>
    /// 获取全局唯一值
    /// </summary>
    /// <returns>string</returns>
    public static string Unique()
    {
        return Guid.NewGuid().ToString().Replace("-", "");
    }

    #endregion

    #region 上传配置

    /// <summary>
    ///  根据文件类型分配路径
    /// </summary>
    /// <param name="fileExt"></param>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string AssigendPath(string fileExt, string path)
    {
        var dataPath = DateTime.Now.ToString("yyyyMMdd");
        if (IsImage(fileExt))
            return path + "/upload/images/" + dataPath + "/";
        if (IsVideos(fileExt))
            return path + "/upload/videos/" + dataPath + "/";
        if (IsDocument(fileExt))
            return "/upload/files/" + dataPath + "/";
        if (IsMusics(fileExt))
            return "/upload/musics/" + dataPath + "/";
        return path + "/upload/others/";
    }

    /// <summary>
    /// 是否为图片
    /// </summary>
    /// <param name="fileExt">文件扩展名，不含“.”</param>
    /// <returns></returns>
    private static bool IsImage(string fileExt)
    {
        var images = new List<string> { "bmp", "gif", "jpg", "jpeg", "png" };
        return images.Contains(fileExt.ToLower());
    }

    /// <summary>
    /// 是否为视频
    /// </summary>
    /// <param name="fileExt">文件扩展名，不含“.”</param>
    /// <returns></returns>
    private static bool IsVideos(string fileExt)
    {
        var videos = new List<string>
        { "rmvb", "mkv", "ts", "wma", "avi", "rm", "mp4", "flv", "mpeg", "mov", "3gp", "mpg" };
        return videos.Contains(fileExt.ToLower());
    }

    /// <summary>
    /// 是否为音频
    /// </summary>
    /// <param name="fileExt">文件扩展名，不含“.”</param>
    /// <returns></returns>
    private static bool IsMusics(string fileExt)
    {
        var musics = new List<string> { "mp3", "wav" };
        return musics.Contains(fileExt.ToLower());
    }

    /// <summary>
    /// 是否为文档
    /// </summary>
    /// <param name="fileExt">文件扩展名，不含“.”</param>
    /// <returns></returns>
    private static bool IsDocument(string fileExt)
    {
        var documents = new List<string> { "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "pdf" };
        return documents.Contains(fileExt.ToLower());
    }

    #endregion


}
